//
//  ToolbarShapeItemView.m
//  HpnotiqCustomize
//
//  Created by Harmandeep Singh on 09/06/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ToolbarShapeItemView.h"
#import <QuartzCore/QuartzCore.h>


@implementation ToolbarShapeItemView

@synthesize mIsBeingMoved;
@synthesize mOriginalFrameInToolbar;

- (id)initWithFrame:(CGRect)frame andSVGFileName:(NSString *)fileName
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        mSVGFileName = [fileName copy];
        
        SVGDocument *document = [SVGDocument documentNamed:[mSVGFileName stringByAppendingPathExtension:@"svg"]];
        
       
        
        
        
        mSVGView = [[SVGView alloc] initWithDocument:document];
        mSVGView.userInteractionEnabled = NO;
        
        CGFloat svgViewWidth = mSVGView.frame.size.width;
        CGFloat svgViewHeight = mSVGView.frame.size.height;
        CGFloat ratio = svgViewWidth / svgViewHeight;
        CGFloat reductionFactor = 1;
        
        if(ratio < 1)
        {
            // Height is more
            reductionFactor = frame.size.height / svgViewHeight;
        }
        else
        {
            // Width is more
            reductionFactor = frame.size.width / svgViewWidth;
        }
        mSVGView.transform = CGAffineTransformMakeScale(reductionFactor, reductionFactor);
        mSVGView.frame = CGRectMake(0,
                                    0,
                                    mSVGView.frame.size.width,
                                    mSVGView.frame.size.height);
        [self addSubview:mSVGView];
        
        
        
        
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


- (void)dealloc {
    
    [mSVGFileName release];
    [mSVGView release];
    [super dealloc];
}




- (SVGView *)clone
{
    SVGDocument *document = [SVGDocument documentNamed:[mSVGFileName stringByAppendingPathExtension:@"svg"]];
    SVGView *newSVGView = [[[SVGView alloc] initWithDocument:document] autorelease];

    
    newSVGView.userInteractionEnabled = NO;
    
    CGFloat svgViewWidth = newSVGView.frame.size.width;
    CGFloat svgViewHeight = newSVGView.frame.size.height;
    CGFloat ratio = svgViewWidth / svgViewHeight;
    CGFloat reductionFactor = 1;
    
    if(ratio < 1)
    {
        // Height is more
        reductionFactor = mSVGView.frame.size.height / svgViewHeight;
    }
    else
    {
        // Width is more
        reductionFactor = mSVGView.frame.size.width / svgViewWidth;
    }
    newSVGView.transform = CGAffineTransformMakeScale(reductionFactor, reductionFactor);
    newSVGView.frame = CGRectMake(0,
                                  0,
                                  newSVGView.frame.size.width,
                                  newSVGView.frame.size.height);
    
    return newSVGView;
}


@end
